<?php


namespace App\Http\Controllers\Admin;


use App\Model\Order;
use App\Tool\Json;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class OrderController extends AuthCheckController
{

    /**
     * 订单列表
     * Create by PeterYang
     * 2020/07/04 18:07:17
     * Email:904801074@qq.com
     * @return array
     */
    function order_list(){

        $list=Order::with('user')
            ->with('address')
            ->with('address_copy')
            ->with('store_copy')
            ->with('order_transaction')->with('order_goods')->orderBy('id','desc');


        $list=paginate($list,request()->input('p'));

        return $list;
    }

    /**
     * 订单设置存储
     * Create by Peter
     * 2020/06/19 11:00:55
     * Email:904801074@qq.com
     */
    function order_setting_save(){

        $post=request()->input();

        foreach ($post as $key=>$value){

            if($key=='phone_list'){

                //中文逗号转英文逗号
                $value=str_replace('，',',',$value);

            }

            Cache::put('order_setting:'.$key,$value);

        }


        return Json::code(1,'success');

    }


    function order_setting_detail(){


        $data=[];

        $data['order_wait_time']=Cache::get('order_setting:order_wait_time',config('order.order_wait_time'));
        $data['sms_sign_name']=Cache::get('order_setting:sms_sign_name');
        $data['order_is_pay_notice']=Cache::get('order_setting:order_is_pay_notice');
        $data['order_finished_notice']=Cache::get('order_setting:order_finished_notice');
        $data['order_refund_notice']=Cache::get('order_setting:order_refund_notice');
        $data['order_refund_finished_notice']=Cache::get('order_setting:order_refund_finished_notice');
        $data['phone_list']=Cache::get('order_setting:phone_list');


        return Json::code(1,'success',$data);
    }


    /**
     * @Auth(type='no_check')
     * 手机端查看订单
     * Create by PeterYang
     * 2020/07/04 18:08:46
     * Email:904801074@qq.com
     */
    function need_mention_order_list(){

//        explode()

        $list=Order::leftJoin('order_mention','order_mention.order_id','=','order.id')
            ->select('order.*')
            ->with('order_goods.goods')
            ->with('user')
//            ->whereT
            ->with('order_mention')
            ->orderBy('order_mention.start_time','desc')
            ->where('status',11)
            ->where('delivery_type',2);


        //是否只显示今日
        if(request()->input('is_today')){

            $list->whereHas('order_mention',function ( $query){


                $query->whereDate('end_time','=',now()->format('Y-m-d'));
            });

        }



        return Json::code(1,'success',$list->get());

    }


}
